﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class TopicGroup : BaseItem
    {
        public TopicGroup() : base() { }
        private TopicGroup(Hashtable table) : base(table) { }
        private TopicGroup(Hashtable table, List<TopicGroup> collection) : base(table, collection) { }

        public static TopicGroup CreateInstance()
        {
            return new TopicGroup();
        }

        protected override Acms.SqlDal.Base CreateDal()
        {
            return new TopicGroupDal();
        }

        protected override void Load(System.Collections.Hashtable table)
        {
            Id = (int)table[TopicGroupDal.Fields.Id];
            SiteId = (int)table[TopicGroupDal.Fields.SiteId];
            Name = (string)table[TopicGroupDal.Fields.Name];
            Path = (string)table[TopicGroupDal.Fields.Path];
            Notes = (string)table[TopicGroupDal.Fields.Notes];
            TopicCount = (int)table[TopicGroupDal.Fields.TopicCount];
            Created = (DateTime)table[TopicGroupDal.Fields.Created];
        }

        public int Id { get; private set; }
        public int SiteId { get; set; }
        public string Name { get; set; }
        public string Path { get; set; }
        public string Notes { get; set; }
        public int TopicCount { get; private set; }
        public DateTime Created { get; private set; }

        private static TopicGroup Get(Hashtable table)
        {
            if (table != null)
            {
                return new TopicGroup(table);
            }
            else
            {
                return null;
            }
        }
    
        private static List<TopicGroup> Get(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<TopicGroup> tmp = new List<TopicGroup>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new TopicGroup(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        public static TopicGroup GetById(int id)
        {
            TopicGroupDal dal = new TopicGroupDal();
            return Get(dal.GetById(id));
        }

        public static List<TopicGroup> GetAllBySiteId(int siteId)
        {
            TopicGroupDal dal = new TopicGroupDal();
            return Get(dal.GetAllBySiteId(siteId));
        }

        public static TopicGroup GetBySiteIdAndPath(int siteId, string path)
        {
            TopicGroupDal dal = new TopicGroupDal();
            return Get(dal.GetBySiteIdAndPath(siteId, path));
        }


        public void Update()
        {
            if (IsNewRecord)
            {
                Created = DateTime.Now;
            }
            base.Update();
        }

        protected override object SaveChanges(ref object concurencyId, Base dal)
        {
            int id = Id;

            ((TopicGroupDal)dal).Update(ref id, SiteId, Name, Path, Notes, Created, ref concurencyId);

            Id = id;
            return Id;
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((TopicGroupDal)dal).Delete(Id, concurencyId);
        }

        private List<Topic> topics;
        public List<Topic> Topics
        {
            get 
            {
                if (topics == null && this.Id != 0)
                {
                    topics = Topic.GetAllByTopicGroupId(Id);
                }
                return topics;
            }
        }

    }
}
